/**
	*****************************************************************************
	* @file     cmem7_gpio.c
	*
	* @brief    CMEM7 GPIO source file
	*
	*
	* @version  V1.0
	* @date     3. September 2013
	*
	* @note               
	*           
	*****************************************************************************
	* @attention
	*
	* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
	* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
	* TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 
	* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
	* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
	* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
	*
	* <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
	*****************************************************************************
	*/
	
#include "cmem7_gpio.h"

void GPIO_Init(uint8_t Group, uint32_t PositiveTrigger) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  GPIO->GPIO_POSITIVE_EDGE_INT_TRIGGER = PositiveTrigger;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_H_POSITIVE_EDGE_INT_TRIGGER = PositiveTrigger;
	} else {
		GPIO->GPIO_N_POSITIVE_EDGE_INT_TRIGGER = PositiveTrigger;
	}
}

void GPIO_EnableOutput(uint8_t Group, uint32_t Enable) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  GPIO->GPIO_OE = Enable;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_H_OE = Enable;
	} else {
		GPIO->GPIO_N_OE = Enable;
	}
}

void GPIO_EnableInt(uint8_t Group, uint32_t Enable) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  GPIO->GPIO_INT_MASK = ~Enable;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_INT_MASK = ~Enable;
	} else {
		GPIO->GPIO_INT_MASK = ~Enable;
	}
}

uint32_t GPIO_Read(uint8_t Group) {
  uint32_t data;
	
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  data = GPIO->GPIO_IN;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		data = GPIO->GPIO_H_IN;
	} else {
		data = GPIO->GPIO_N_IN;
	}
	
	return data;
}

void GPIO_Write(uint8_t Group, uint32_t Unmask, uint32_t data) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
		GPIO->GPIO_OUT_UNMASK = Unmask;
	  GPIO->GPIO_OUT_DATA = data;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_H_OUT_UNMASK = Unmask;
	  GPIO->GPIO_H_OUT_DATA = data;
	} else {
		GPIO->GPIO_N_OUT_UNMASK = Unmask;
	  GPIO->GPIO_N_OUT_DATA = data;
	}
}

void GPIO_InitPwm(uint8_t Channel, uint32_t HighLevelNanoSecond, uint32_t LowLevelNanoSecond) {
	uint16_t lowTick, highTick;
	
	assert_param(IS_GPIO_PWM_CHANNEL(Channel));
	
	lowTick = LowLevelNanoSecond * (SYSTEM_CLOCK_FREQ / 1000000);
	highTick = HighLevelNanoSecond * (SYSTEM_CLOCK_FREQ / 1000000);
	if (Channel == GPIO_PWM_CHANNEL_GPIO_31) {
		GPIO->PWM_OUT0_LEN_b.LOW_LEVEL_TICK = lowTick; 
		GPIO->PWM_OUT0_LEN_b.HIGH_LEVEL_TICK = highTick; 
	} else if (Channel == GPIO_PWM_CHANNEL_GPIO_H_9) {
		GPIO->PWM_OUT1_LEN_b.LOW_LEVEL_TICK = lowTick; 
		GPIO->PWM_OUT1_LEN_b.HIGH_LEVEL_TICK = highTick; 
	} else if (Channel == GPIO_PWM_CHANNEL_GPIO_H_19) {
		GPIO->PWM_OUT2_LEN_b.LOW_LEVEL_TICK = lowTick; 
		GPIO->PWM_OUT2_LEN_b.HIGH_LEVEL_TICK = highTick; 
  } else {
		GPIO->PWM_OUT3_LEN_b.LOW_LEVEL_TICK = lowTick; 
		GPIO->PWM_OUT3_LEN_b.HIGH_LEVEL_TICK = highTick; 
	}
}

void GPIO_EnablePwm(uint8_t Channel, uint32_t Enable) {
	assert_param(IS_GPIO_PWM_CHANNEL(Channel));
	
	if (Channel == GPIO_PWM_CHANNEL_GPIO_31) {
		GPIO->PWM_OUT_EN_b.GPIO_31 = Enable; 
	} else if (Channel == GPIO_PWM_CHANNEL_GPIO_H_9) {
		GPIO->PWM_OUT_EN_b.GPIO_H_9 = Enable; 
	} else if (Channel == GPIO_PWM_CHANNEL_GPIO_H_19) {
		GPIO->PWM_OUT_EN_b.GPIO_H_19 = Enable; 
  } else {
		GPIO->PWM_OUT_EN_b.GPIO_H_20 = Enable; 
	}
}

